/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.custom;

import java.io.File;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;

import javolution.util.FastMap;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.l2jserver.gameserver.templates.StatsSet;

/**
 * @author Laikeriz
 */
public class MessageConfigManager
{
	public static Map<Integer, StatsSet> everything = new FastMap<Integer, StatsSet>();
	public static int currentLang = 1;
	
	public static Message getMessage(String name, int langId)
	{
		return new Message(everything.get(langId).getString(name));
	}
	
	public static void init()
	{
		try
		{
			File file = new File("config/messages.xml");
			Document doc = null;
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating(false);
			factory.setIgnoringComments(true);
			doc = factory.newDocumentBuilder().parse(file);
			for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
			{
				if ("list".equalsIgnoreCase(n.getNodeName()))
				{
					for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
					{
						if ("current".equalsIgnoreCase(d.getNodeName()))
							currentLang = Integer.parseInt(d.getAttributes().getNamedItem("lang").getNodeValue());
						if ("lang".equalsIgnoreCase(d.getNodeName()))
						{
							StatsSet tempSet = new StatsSet();
							String lang = d.getAttributes().getNamedItem("lang").getNodeValue();
							int langId = Integer.parseInt(d.getAttributes().getNamedItem("id").getNodeValue());
							tempSet.set("langId", langId);
							tempSet.set("langName", lang);
							for (Node l = d.getFirstChild(); l != null; l = l.getNextSibling())
							{
								if ("message".equalsIgnoreCase(l.getNodeName()))
								{
									String name = l.getAttributes().getNamedItem("name").getNodeValue();
									String text = l.getAttributes().getNamedItem("text").getNodeValue();
									tempSet.set(name, text);
								}
							}
							everything.put(langId, tempSet);
						}
					}
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
